﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class UIWTZ : ViewBase
    {
        #region ==TBDF===========
        public static readonly DependencyProperty TBDFProperty = DependencyProperty.Register("TBDF",
            typeof(DataTable), typeof(UIWTZ),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTBDFChanged));
        public DataTable TBDF
        {
            get
            {
                return (DataTable)GetValue(TBDFProperty);
            }
            set
            {
                SetValue(TBDFProperty, value);
                CHKWEAVE();
            }
        }
        private static void OnTBDFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWTZ;
            if (src != null)
            {
                src.TBDF = (DataTable)e.NewValue;
            }
        }
        #endregion
        #region ==TBPG===========
        public static readonly DependencyProperty TBPGProperty = DependencyProperty.Register("TBPG",
            typeof(DataTable), typeof(UIWTZ),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTBPGChanged));
        public DataTable TBPG
        {
            get
            {
                return (DataTable)GetValue(TBPGProperty);
            }
            set
            {
                SetValue(TBPGProperty, value);
                CHKWEAVE();
            }
        }
        private static void OnTBPGChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWTZ;
            if (src != null)
            {
                src.TBPG = (DataTable)e.NewValue;
            }
        }
        #endregion
        #region ==LMS===========
        public static readonly DependencyProperty LMSProperty = DependencyProperty.Register("LMS",
            typeof(string), typeof(UIWTZ),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnLMSChanged));
        public string LMS
        {
            get
            {
                return (string)GetValue(LMSProperty);
            }
            set
            {
                SetValue(LMSProperty, value);
            }
        }
        private static void OnLMSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWTZ;
            if (src != null)
            {
                src.LMS = (string)e.NewValue;
            }
        }
        #endregion
        #region ==EEZ===========
        public static readonly DependencyProperty EEZProperty = DependencyProperty.Register("EEZ",
            typeof(string), typeof(UIWTZ),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEEZChanged));
        public string EEZ
        {
            get
            {
                return (string)GetValue(EEZProperty);
            }
            set
            {
                SetValue(EEZProperty, value);
                OnEEZChg();
            }
        }
        private static void OnEEZChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWTZ;
            if (src != null)
            {
                src.EEZ = (string)e.NewValue;
            }
        }
        public virtual void OnEEZChg()
        { }
        #endregion

        #region ==DFS===========
        public static readonly DependencyProperty DFSProperty = DependencyProperty.Register("DFS",
            typeof(string), typeof(UIWTZ),
            new FrameworkPropertyMetadata("4", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnDFSChanged));
        public string DFS
        {
            get
            {
                return (string)GetValue(DFSProperty);
            }
            set
            {
                SetValue(DFSProperty, value);
                OnDFSChg(value);
            }
        }
        private static void OnDFSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWTZ;
            if (src != null)
            {
                src.DFS = (string)e.NewValue;
            }
        }
        public virtual void OnDFSChg(string dfs)
        {
            if (!IsEdit)
                return;
            if(TBMain.Rows.Count==0)
                return;
            DataRow dwP = TBMain.Rows[0];
            dwData = TMV.VSD.TBMain.NewRow();
            dwData["SDSA"] = dwP["SDSA"];
            dwData["SDKEY"] = dwP["SDKEY"];
            string vu = dwP["SDDATEN"].ToString();
            dwData["SDDATEN"] = vu.Substring(0, 61) +Helper.Right("00000000"+DFS,8)+  vu.Substring(69);
            TMV.VSD.UpdateSDDAT(dwData, app.company);
        }
        #endregion

        #region ==DFPS===========
        public static readonly DependencyProperty DFPSProperty = DependencyProperty.Register("DFPS",
            typeof(string), typeof(UIWTZ),
            new FrameworkPropertyMetadata("4", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnDFPSChanged));
        public string DFPS
        {
            get
            {
                return (string)GetValue(DFPSProperty);
            }
            set
            {
                SetValue(DFPSProperty, value);
                OnDFPSChg();
            }
        }
        private static void OnDFPSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWTZ;
            if (src != null)
            {
                src.DFPS = (string)e.NewValue;
            }
        }

        #endregion
        #region ==PGPS===========
        public static readonly DependencyProperty PGPSProperty = DependencyProperty.Register("PGPS",
            typeof(string), typeof(UIWTZ),
            new FrameworkPropertyMetadata("4", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPGPSChanged));
        public string PGPS
        {
            get
            {
                return (string)GetValue(PGPSProperty);
            }
            set
            {
                SetValue(PGPSProperty, value);
                OnPGPSChg();
            }
        }
        private static void OnPGPSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWTZ;
            if (src != null)
            {
                src.PGPS = (string)e.NewValue;
            }
        }

        #endregion

        #region ==BIND===========
        public static readonly DependencyProperty BINDProperty = DependencyProperty.Register("BIND",
            typeof(string), typeof(UIWTZ),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnBINDChanged));
        public string BIND
        {
            get
            {
                return (string)GetValue(BINDProperty);
            }
            set
            {
                SetValue(BINDProperty, value);
            }
        }
        private static void OnBINDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWTZ;
            if (src != null)
            {
                src.BIND = (string)e.NewValue;
            }
        }

        #endregion
        #region ==PEGPLAN===========
        public static readonly DependencyProperty PEGPLANProperty = DependencyProperty.Register("PEGPLAN",
            typeof(string), typeof(UIWTZ),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPEGPLANChanged));
        public string PEGPLAN
        {
            get
            {
                return (string)GetValue(PEGPLANProperty);
            }
            set
            {
                SetValue(PEGPLANProperty, value);
            }
        }
        private static void OnPEGPLANChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWTZ;
            if (src != null)
            {
                src.PEGPLAN = (string)e.NewValue;
            }
        }

        #endregion
        #region ==WEAVE===========
        public static readonly DependencyProperty WEAVEProperty = DependencyProperty.Register("WEAVE",
            typeof(string), typeof(UIWTZ),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWEAVEChanged));
        public string WEAVE
        {
            get
            {
                return (string)GetValue(WEAVEProperty);
            }
            set
            {
                SetValue(WEAVEProperty, value);
            }
        }
        private static void OnWEAVEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWTZ;
            if (src != null)
            {
                src.WEAVE = (string)e.NewValue;
            }
        }

        #endregion

        public VWTEINZ TMV
        {
            get
            {
                return (VWTEINZ)MV;
            }
        }
        public UIWTZ()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return; 
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);

        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);

            if (dwMaster == null)
            {
                KEY = "";
                TXTISIND = "";
            }
            else
            {
                KEY = TMV.WTEEINZNR;
                TXTISIND = KEY.PadRight(24);
            }
        } 
        public override void LOADBYAUF()
        {
            if (IsCanLoad)
                return;
            TBMain.Clear(); 
            if (ORDERNUM.Length < 15)
                return;
            PM["EINZNR"] = ORDERNUM.Substring(0, 15);
            SResult = TMV.LOADBYWS(PM);
            MergeData(SResult, true);
            base.LOADBYAUF();
        }

        public virtual void CHKWEAVE()
        { }
        //穿综变化
        public virtual void OnDFPSChg()
        {
        
        }
        //纹板变化
        public virtual void OnPGPSChg()
        {
            
        }
        public virtual void ByteChgCommand(object sender, MouseButtonEventArgs e)
        {
        }
        public void WVCommand(object sender, RoutedEventArgs e)
        {
            VICC_WFEVENTS.SVWF.WT06(RUNID, KEY);
        }
        public void ShowHide(string _dfs)
        {
            DataGridTemplateColumn cDFX = null;
            string cDFn = "cDF";
            int dfs = Convert.ToInt16(_dfs);
            for (int i = 1; i <= 16; i++)
            {
                cDFn = "cDF" + Helper.Right("00" + i, 2);

                cDFX = (DataGridTemplateColumn)this.FindName(cDFn);
                if (cDFX == null)
                    continue;
                cDFX.Visibility = System.Windows.Visibility.Hidden;
                if (i < dfs)
                {
                    cDFX.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {

                }
            }
        }
        public void InitialTable(string _dfs)
        {

            ShowHide(_dfs);  
            int count = TBMain.Rows.Count;
            if (count == 0)
                return;
            int dfs = Convert.ToInt16(_dfs);
            DataRow dwTemp = TBMain.Clone().NewRow();
            dwTemp.ItemArray = TBMain.Rows[0].ItemArray;
            dwTemp["LINE"] = 0;
            for (int idx = 0; idx < dfs; idx++)
            {
                string pos = Helper.Right("0000" + idx, 4);
                dwTemp[LMS + pos] = 0;//初始化
            }
            TBMain.Clear();
            TBMain.AcceptChanges();
            TMV.VTK.DTK02(dwTemp);
            for (int idx = 1; idx <= dfs; idx++)
            {
                dwData = TBMain.Rows.Add();
                dwData.ItemArray = dwTemp.ItemArray;
                dwData["LINE"] = idx * 10;
                dwData["TXTKPOS"] = dwData["LINE"];
                TMV.VTK.BATCHWB14(dwData);
            }
            TMV.VTK.POSTCHG(); 
        }
        public void LOADPATTERN(DataSet SResult,int dfs)
        {
            ShowHide(dfs.ToString());
            string pattern = "";
            string pos = "";
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                double line = Convert.ToDouble(row["TXTKPOS"].ToString());
                pattern = row["TXTKTEXT"].ToString();
                int lg = pattern.Trim().Length;
                string pv = "0";
                for (int idx = 0; idx < dfs; idx++)
                {
                    pos = Helper.Right("0000" + idx, 4);
                    pv = pattern.Substring(idx, 1);
                    if (pv == " ")
                        pv = "0";
                    row["LINE"] = (idx + 1) * 10;
                    row[LMS + pos] = pv;
                }
            }
            MergeData(SResult, true);
        }
    }
}
